package com.stork.sms.activity;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.stork.sms.R;
import com.stork.sms.adapter.MarkListPagerAdapter;
import com.stork.sms.service.marklist.ServiceCall;

public class MarkList extends Activity {
	
	
	String Selecteditem;	
	public String[] Results;		
	public List<String> examNames=new ArrayList<String>();
	public ArrayAdapter<String> examTypeAdapter;
	Spinner ExamTypes;	
	ViewPager markListViewPager;
	public int viewPagerSize, spinnerPosition;
    MarkListPagerAdapter marklistAdapter;
	Button chartButton;
	public ProgressDialog marksProgressBar;
	private Handler handler=new Handler();
	private int progressStatus;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mark_list);
		
		
		chartButton=(Button)findViewById(R.id.submitbutton1);
		ExamTypes=(Spinner)findViewById(R.id.Exams_list);
		markListViewPager=(ViewPager)findViewById(R.id.viewPager);
		
		ActionBar ab=getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);
		ab.setTitle("Contacus");
				
		new GetMarks().execute();				
		
markListViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				
				if(arg0==0)
				{
				ExamTypes.setSelection(0);		
			}
				else
			{
					ExamTypes.setSelection(1);					
			}
				
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}			
		});

		ExamTypes.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
			
				 Selecteditem=ExamTypes.getSelectedItem().toString();				 
				 //new GetMarks().execute();
				 markListViewPager.setAdapter(marklistAdapter);
				
				if(position==0)
				 {					
					markListViewPager.setCurrentItem(0);
					spinnerPosition=position;
				
				}else 
				{					
					markListViewPager.setCurrentItem(1);	 
					spinnerPosition=position;
				}
				/*TableRow nr = (TableRow)LayoutInflater.from(MarkList.this).inflate(R.layout.nameregno, null);
			    ((TextView)nr.findViewById(R.id.name)).setText(name[position]);
			    ((TextView)nr.findViewById(R.id.regno)).setText(regno[position]);
			MarkListTable.addView(nr,new TableLayout.LayoutParams(
	                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));*/
			
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
				
			}
		});
		
chartButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			//new GetMarks().execute();
				if(spinnerPosition==0)
				{
				double[] chartmarkvalues=new double[] {80, 80};
				openChart(chartmarkvalues);
					
				}else
				{
					double[] chartmarkvalues=new double[] {60, 50};
					openChart(chartmarkvalues);
				}
			}
		});
		
	}
		
		 private void openChart(double[] marks){
			 
		        // Pie Chart Section Names
		        String[] SubjectName = new String[] {"Tamil", "English"};
		 
		        // Pie Chart Section Value
		        double[] MarkValues = marks;
		 
		        // Color of each Pie Chart Sections
		        int[] colors = { Color.BLUE, Color.MAGENTA};
		 
		        // Instantiating CategorySeries to plot Pie Chart
		        CategorySeries distributionSeries = new CategorySeries(" Student Exam Marks Details Academy Year 2014");
		        for(int i=0 ;i < MarkValues.length;i++){
		            // Adding a slice with its values and name to the Pie Chart
		            distributionSeries.add(SubjectName[i], MarkValues[i]);
		        }
		 
		        // Instantiating a renderer for the Pie Chart
		        DefaultRenderer defaultRenderer  = new DefaultRenderer();
		        for(int i = 0 ;i<MarkValues.length;i++){
		            SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
		            seriesRenderer.setColor(colors[i]);		            
		            seriesRenderer.setDisplayChartValues(true);
		            seriesRenderer.setChartValuesTextSize(20.05f);
		            // Adding a renderer for a slice
		            defaultRenderer.addSeriesRenderer(seriesRenderer);
		        }
		 
		        defaultRenderer.setChartTitle("Student Exam Marks Details Academy Year 2014");
		        defaultRenderer.setChartTitleTextSize(20);		        
		        defaultRenderer.setZoomButtonsVisible(true);		        
		 
		        // Creating an intent to plot bar chart using dataset and multipleRenderer
		        Intent intent = ChartFactory.getPieChartIntent(getBaseContext(), distributionSeries , defaultRenderer, "AChartEnginePieChartDemo");
		 
		        // Start Activity
		        startActivity(intent);	    
	
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.mark_list, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch(item.getItemId())
		{
		
		case android.R.id.home:
			super.onBackPressed();
			return true;
			
		case R.id.action_logout:
			
			Intent logout=new Intent(MarkList.this, Login.class);
			startActivity(logout);
			return true;
			
		case R.id.action_contactus:
			
			Intent contactus=new Intent(MarkList.this, Contactus.class);
			startActivity(contactus);
			return true;
			
		}			
			return super.onOptionsItemSelected(item);
		}
	class GetMarks extends AsyncTask<String, Void, String[]>
	{
@Override
protected void onPreExecute() {
	// TODO Auto-generated method stub
	super.onPreExecute();
	marksProgressBar=new ProgressDialog(MarkList.this);
	marksProgressBar.setMessage("Please Wait");
	marksProgressBar.setCancelable(false);
	  marksProgressBar.show();}
		@Override
		protected String[] doInBackground(String... params) {
			// TODO Auto-generated method stub
		
			ServiceCall SC=new ServiceCall();			
			String id="DAV012K14LKG1";
			Results=SC.GetMarks(id);			
			System.out.println(Results[0]);
			System.out.println(Results[1]);
			System.out.println(Results[9]);
			System.out.println(Results.length);
			return Results;
		}
		
	
	@Override
	protected void onPostExecute(String[] result) {
		// TODO Auto-generated method stub				
		if(marksProgressBar.isShowing())
		{
			marksProgressBar.dismiss();
		}
		if(Results!=null)
		{		
			int count=4;
			for(int i=0;i<=Results.length;i++)
			{
				if(count<Results.length)
				{
				examNames.add(Results[count]);
				System.out.print(Results[count]);
				count=count+5;
				}
			}
		}
		viewPagerSize=examNames.size();
		examTypeAdapter = new ArrayAdapter<String>(MarkList.this, android.R.layout.simple_spinner_item, examNames);
		examTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
				ExamTypes.setAdapter(examTypeAdapter);
				
		marklistAdapter=new MarkListPagerAdapter(MarkList.this, Results, viewPagerSize);	
		
	}
		
	}

}
